function getMid() {
    return JSON.parse(sessionStorage.getItem("manageInfo")).mid;
}

layui.use(['layer', 'form', 'element'], function () {
    var layer = layui.layer;
    var element = layui.element;
    var form = layui.form;

    (function ($) {
        funObj = {
            timeUserFun: 'timeUserFun',
        }
        $[funObj.timeUserFun] = function (time) {
            var time = time || 2;    // 默认参数
            var userTime = time * 60;
            var objTime = {
                init: 0,
                time: function () {
                    objTime.init += 1;
                    if (objTime.init == userTime) {
                        // 用户到达未操作事件 做一些处理
                        update();
                    }
                },
                eventFun: function () {
                    clearInterval(testUser);
                    objTime.init = 0;
                    testUser = setInterval(objTime.time, 1000);
                }
            }

            var testUser = setInterval(objTime.time, 1000);

            var body = document.querySelector('html');
            body.addEventListener("click", objTime.eventFun);
            body.addEventListener("keydown", objTime.eventFun);
            body.addEventListener("mousemove", objTime.eventFun);
            body.addEventListener("mousewheel", objTime.eventFun);
        }
    })(window)

    function update() {
        let mid = getMid();
        $.ajax({
            url: "/future/manager/saveLoginStatus/" + mid,
            type: "POST",
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            async: false,
            success(res) {
                if (res.code == "200") {
                    sessionStorage.setItem("manageInfo", "null");
                    layer.msg("由于您长时间未操作页面，请重新登录", {icon: 2, time: 2000});
                    setTimeout(function () {
                        window.location.reload();
                    }, 2000);
                }
            }
        })
    }

    // 直接调用 参数代表分钟数,可以有一位小数，不传参默认2分钟
    timeUserFun(20);

    let vm = new Vue({
        el: "#page",
        data: {
            manageInfo: {},
            menuList: [],
            htmlpath: "/future/api/wel",
            _beforeUnload_time: 0,
            _gap_time:0
        },
        mounted() {
            window.addEventListener('beforeunload', e => this.beforeunloadHandler(e))
            window.addEventListener('unload', e => this.unloadHandler(e))
            this.manageInfo = JSON.parse(sessionStorage.getItem("manageInfo"));
            if (undefined == this.manageInfo || null == this.manageInfo) {
                layer.msg("登录状态已过期，请重新登录", {icon: 2, time: 2000});
                setTimeout(function (){
                    window.location.href = "/future/api/login";
                },2000);
            } else {
                $("#managername").html(this.manageInfo.mno);
                this.manageInfo.mrole === 0 ? layer.msg("欢迎管理员" + this.manageInfo.mno + "登录", {
                    icon: 1,
                    time: 2000
                }) : layer.msg("欢迎超级管理员:" + this.manageInfo.mno + "登录", {icon: 1, time: 2000});
            }
            this.getMenuList(this.manageInfo.mid);
        },
        destroy() {
        },
        methods: {
            beforeunloadHandler(e) {
                e = e || window.event;
                if (e) {
                    this._beforeUnload_time = new Date().getTime();
                    update();
                    e.returnValue = '关闭提示';
                }
                update();
                this._beforeUnload_time = new Date().getTime();
                return '关闭提示';
            },
            unloadHandler(e) {
                this._gap_time = new Date().getTime() - this._beforeUnload_time;
                //判断是窗口关闭还是刷新
                if (this._gap_time <= 5) {
                   update();
                   return false;
                }
            },
            getMenuList(managerid) {
                var that = this;
                $.ajax({
                    url: "/future/manager/getMenuTree/" + managerid,
                    dataType: "json",
                    async: false,
                    success(res) {
                        that.menuList = res.objList;
                    }
                })
            }
            ,
            setAframe(path, isTitle, i) {
                if (isTitle) {
                    let navTitle = this.$refs.model[i];
                    if ("" != path && null != path && undefined != path) {
                        this.htmlpath = path;
                    }
                    if (navTitle.className.indexOf("layui-nav-itemed") > -1) {
                        $(navTitle).removeClass("layui-nav-itemed");
                    } else {
                        $(navTitle).addClass("layui-nav-itemed");
                    }
                } else {
                    this.htmlpath = path;
                }
            }

        }
    })
});
